﻿using JESAI.Application.Abstractions.Sys;
using JESAI.Application.Abstractions.Sys.Dtos;
using JESAI.AutoMapper.ObjectMapping;
using JESAI.Core.Attributes;
using JESAI.Platform.Domain.Sys.Entities;
using JESAI.Platform.Domain.Sys.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JESAI.Core.Util.Extensions.Strings;

namespace JESAI.Application.Service.Sys
{
    [AppService]
    internal class FormDataSourceService : IFormDataSourceService
    {
        private readonly IFormDataSourceRepository _formDataSourceRepository;
        private readonly IFormFieldRepository _formFieldRepository;

        public FormDataSourceService(IFormDataSourceRepository formDataSourceRepository, IFormFieldRepository formFieldRepository)
        {
            _formDataSourceRepository = formDataSourceRepository;
            _formFieldRepository = formFieldRepository;
        }

        public async Task<List<FormDataSourceRefDto>> GetFormColumnRefDataSourceList(string formId)
        {
            var formDataSourceList = _formDataSourceRepository._DbQueryable.Where(x => x.FormId == formId).ToList();
            if (formDataSourceList.Count() > 0)
            {
                return formDataSourceList.MapTo<List<FormDataSourceRefDto>>();
            }


            var formFieldList = _formFieldRepository._DbQueryable.Where(x => x.FormId == formId).ToList();
            if (formFieldList.Count() > 0)
            {
                var formDataSourceRefList = new List<FormDataSourceRefDto>();
                foreach (var formField in formFieldList)
                {
                    var formDataSourceRef = new FormDataSourceRefDto()
                    {
                        FormId = formField.FormId,
                        FormField = formField.Field,
                        FormTitle = formField.Title,
                        FormType = formField.Type
                    };
                    formDataSourceRefList.Add(formDataSourceRef);
                }
                return formDataSourceRefList;
            }

            return null;

        }

        public async Task SaveOrUpdateFormDataSourceRef(List<FormDataSourceRefInput> formDataSourceRefInputs)
        {
            var tableIds = formDataSourceRefInputs.Select(x => x.TableId).Distinct();
            var formDataSourceRefs = _formDataSourceRepository._DbQueryable.Where(x => tableIds.Contains(x.TableId)).ToList();
            if (formDataSourceRefs.Count() > 0)
            {
                //查找删除的,有的数据并不需要保存，就删除
                var deleteFormDataSourceRefIds = formDataSourceRefs.Where(x => !formDataSourceRefInputs.Any(y => y.TableId == x.TableId)).Select(p => p.Id).ToArray();
                if (deleteFormDataSourceRefIds.Count() > 0)
                {
                    await _formDataSourceRepository.DeleteByIdsAsync(deleteFormDataSourceRefIds);
                }
            }

            var newFormDataSourceRefs = formDataSourceRefInputs.MapTo<List<FormDataSourceEntity>>();
            List<FormDataSourceEntity> updateFormDataSourceRefs = new List<FormDataSourceEntity>();
            List<FormDataSourceEntity> insertFormDataSourceRefs = new List<FormDataSourceEntity>();
            foreach (var newFormDataSourceRef in newFormDataSourceRefs)
            {
                if (newFormDataSourceRef.Id.IsNullOrWhiteSpace())
                {
                    newFormDataSourceRef.Id = Guid.NewGuid().ToString();
                    insertFormDataSourceRefs.Add(newFormDataSourceRef);
                }
                else
                {
                    updateFormDataSourceRefs.Add(newFormDataSourceRef);
                }
            }

            await _formDataSourceRepository.InsertRangeAsync(insertFormDataSourceRefs);
            await _formDataSourceRepository.UpdateRangeAsync(updateFormDataSourceRefs);
        }
    }
}
